
#include "../ui.h"
#include "../app_includes.h"

#define TIMER_PERIOD        (300)
#define GUN_TAB_NUM         (1)

extern void panel_add_value_btns(lv_obj_t *panel, lv_obj_t *shortkey_btn[], lv_event_cb_t event_cb, uint16_t *val, uint8_t num);

static int32_t para_change_timeout = -1;

static lv_obj_t *ui_screen;
static lv_obj_t *ui_tabview;
static lv_obj_t *ui_tab[GUN_TAB_NUM];
static lv_timer_t *lv_timer;

static void gun_tab0_screen_create(lv_obj_t *tab);
static void gun_tab0_refresh(lv_timer_t *timer);
static void set_value_event_cb(lv_event_t * e);
static void power_select_refresh(void);
static void select_shortkey_event_cb(lv_event_t * e);
static void select_shortkey_back_event_cb(lv_event_t * e);

void lv_gun_close(void)
{
    lv_timer_del(lv_timer);
}

//app应用入口 
lv_obj_t *lv_gun_open(lv_obj_t *screen)
{
    ui_tabview = lv_tab_screen_create(screen, ui_tab, GUN_TAB_NUM);
    gun_tab0_screen_create(ui_tab[0]); 
    
    lv_timer = lv_timer_create(gun_tab0_refresh, TIMER_PERIOD, NULL);
    gun_group_select(0);
    
    return ui_tabview;
}

#define SHORTKEY_NUM    (6)
static lv_obj_t *shortkey_btn[SHORTKEY_NUM];
static uint16_t shortkey_value[]= {100,200,300,330,380,420};

static lv_obj_t *ui_LabelGunTemp;
static lv_obj_t *ui_LabelGunSetTemp;
static lv_obj_t *ui_SliderGunSet;
static lv_obj_t *ui_SliderGunDuty;
static lv_obj_t *ui_LabelGunDuty;
static lv_obj_t *ui_LabelCurrentVal;
static lv_obj_t *ui_LabelCurrentUnit;
static lv_obj_t *ui_PanelShortKey;
static lv_obj_t *ui_SliderGunFan;
static lv_obj_t *ui_LabelGunFan;

static void gun_tab0_screen_create(lv_obj_t *tab)
{
    ui_LabelGunTemp = lv_label_create(tab);
    lv_obj_set_width( ui_LabelGunTemp, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelGunTemp, LV_SIZE_CONTENT);   /// 1
    lv_obj_align( ui_LabelGunTemp, LV_ALIGN_LEFT_MID, 0, -15);
    lv_label_set_text(ui_LabelGunTemp,"379℃");
    lv_obj_set_style_text_font(ui_LabelGunTemp, &ui_font_FontNumberBig, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_SliderGunSet = lv_slider_create(tab);
    lv_slider_set_range(ui_SliderGunSet, 0,45);
    lv_slider_set_value( ui_SliderGunSet, 38, LV_ANIM_OFF);
    if (lv_slider_get_mode(ui_SliderGunSet)==LV_SLIDER_MODE_RANGE ) lv_slider_set_left_value( ui_SliderGunSet, 0, LV_ANIM_OFF);
    lv_obj_set_width( ui_SliderGunSet, 80);
    lv_obj_set_height( ui_SliderGunSet, 6);
    lv_obj_align( ui_SliderGunSet, LV_ALIGN_RIGHT_MID, 0, -30 );

    lv_obj_set_style_bg_color(ui_SliderGunSet, lv_color_hex(0xFFFFFF), LV_PART_KNOB | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_SliderGunSet, 0, LV_PART_KNOB| LV_STATE_DEFAULT);

    ui_LabelGunSetTemp = lv_label_create(tab);
    lv_obj_set_width( ui_LabelGunSetTemp, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelGunSetTemp, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelGunSetTemp, ui_SliderGunSet, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20 );
    lv_label_set_text(ui_LabelGunSetTemp,"SET 380℃");
    lv_obj_set_style_text_font(ui_LabelGunSetTemp, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_SliderGunDuty = lv_slider_create(tab);
    lv_slider_set_range(ui_SliderGunDuty, 0,100);
    lv_slider_set_value( ui_SliderGunDuty, 50, LV_ANIM_OFF);
    if (lv_slider_get_mode(ui_SliderGunDuty)==LV_SLIDER_MODE_RANGE ) lv_slider_set_left_value( ui_SliderGunDuty, 0, LV_ANIM_OFF);
    lv_obj_set_width( ui_SliderGunDuty, 60);
    lv_obj_set_height( ui_SliderGunDuty, 20);
    lv_obj_align( ui_SliderGunDuty, LV_ALIGN_RIGHT_MID, 0, -65);
    lv_obj_set_style_radius(ui_SliderGunDuty, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    lv_obj_set_style_radius(ui_SliderGunDuty, 0, LV_PART_INDICATOR| LV_STATE_DEFAULT);

    lv_obj_set_style_bg_color(ui_SliderGunDuty, lv_color_hex(0xFF8080), LV_PART_KNOB | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_SliderGunDuty, 0, LV_PART_KNOB| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(ui_SliderGunDuty, 2, LV_PART_KNOB| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(ui_SliderGunDuty, 2, LV_PART_KNOB| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(ui_SliderGunDuty, 2, LV_PART_KNOB| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(ui_SliderGunDuty, 2, LV_PART_KNOB| LV_STATE_DEFAULT);

    ui_LabelGunDuty = lv_label_create(ui_SliderGunDuty);
    lv_obj_set_width( ui_LabelGunDuty, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelGunDuty, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_LabelGunDuty, LV_ALIGN_CENTER );
    lv_label_set_text(ui_LabelGunDuty,"50%");
    lv_obj_set_style_text_color(ui_LabelGunDuty, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelGunDuty, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelGunDuty, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);


    ui_PanelShortKey = lv_obj_create(tab);
    lv_obj_set_width( ui_PanelShortKey, 220);
    lv_obj_set_height( ui_PanelShortKey, 90);
    lv_obj_align( ui_PanelShortKey, LV_ALIGN_BOTTOM_MID, 0, 10);
    lv_obj_clear_flag( ui_PanelShortKey, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_PanelShortKey, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelShortKey, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_side(ui_PanelShortKey, LV_BORDER_SIDE_TOP, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(ui_PanelShortKey, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(ui_PanelShortKey, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_SliderGunFan = lv_slider_create(tab);
    lv_slider_set_range(ui_SliderGunFan, 1,5);
    lv_slider_set_value( ui_SliderGunFan, 3, LV_ANIM_OFF);
    if (lv_slider_get_mode(ui_SliderGunFan)==LV_SLIDER_MODE_RANGE ) lv_slider_set_left_value( ui_SliderGunFan, 0, LV_ANIM_OFF);
    lv_obj_set_width( ui_SliderGunFan, 150);
    lv_obj_set_height( ui_SliderGunFan, 22);
    lv_obj_align( ui_SliderGunFan, LV_ALIGN_LEFT_MID, 0, -65);
    lv_obj_set_style_radius(ui_SliderGunFan, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    lv_obj_set_style_radius(ui_SliderGunFan, 0, LV_PART_INDICATOR| LV_STATE_DEFAULT);

    lv_obj_set_style_bg_color(ui_SliderGunFan, lv_color_hex(0xFF8080), LV_PART_KNOB | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_SliderGunFan, 0, LV_PART_KNOB| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(ui_SliderGunFan, 2, LV_PART_KNOB| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(ui_SliderGunFan, 2, LV_PART_KNOB| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(ui_SliderGunFan, 2, LV_PART_KNOB| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(ui_SliderGunFan, 2, LV_PART_KNOB| LV_STATE_DEFAULT);

    ui_LabelGunFan = lv_label_create(ui_SliderGunFan);
    lv_obj_align( ui_LabelGunFan, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(ui_LabelGunFan,"FAN");
    lv_obj_set_style_text_color(ui_LabelGunFan, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_font(ui_LabelGunFan, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelGunFan, 10, LV_PART_MAIN| LV_STATE_DEFAULT);

    lv_obj_set_flex_flow(ui_PanelShortKey, LV_FLEX_FLOW_ROW_WRAP);
    lv_obj_set_flex_align(ui_PanelShortKey, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START);
    lv_obj_set_flex_grow(ui_PanelShortKey,20);
    panel_add_value_btns(ui_PanelShortKey, shortkey_btn, select_shortkey_back_event_cb, shortkey_value, 6);
    
    
    lv_obj_add_event_cb(ui_SliderGunSet, set_value_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_add_event_cb(ui_SliderGunFan, set_value_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
}

static void gun_tab0_refresh(lv_timer_t *timer)
{
    char str[32];
    uint8_t duty = 0;
    
    strcpy(str, "SET ");
    uint_to_str(str+strlen(str), app_dev.para.run.gun_temp, 0, 3, "℃");
    lv_label_set_text(ui_LabelGunSetTemp, str);

    uint_to_str(str, (uint32_t)app_dev.meter.gun_temp.value, 0, 3, "℃"); //刷新温度
    lv_label_set_text(ui_LabelGunTemp, str);
    
    duty = gun.d.val/10;
    uint_to_str(str, duty, 0, 3, "%");//刷新占空比
    lv_label_set_text(ui_LabelGunDuty, str);
    lv_slider_set_value( ui_SliderGunDuty, duty, LV_ANIM_ON);
    
    //refresh para
    EventBits_t event = xEventGroupWaitBits(sys_event,kSysEvent_UIParaChange,pdTRUE,pdFALSE,0);
    if(event & kSysEvent_UIParaChange) {
        if(app_dev.para.run.gun_temp!=lv_slider_get_value(ui_SliderGunSet)) {
            lv_obj_remove_event_cb(ui_SliderGunSet, set_value_event_cb);
            lv_slider_set_value( ui_SliderGunSet, app_dev.para.run.gun_temp/10, LV_ANIM_OFF);
            lv_obj_add_event_cb(ui_SliderGunSet, set_value_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
        }
        
        if(app_dev.para.run.gun_fan_gear!=lv_slider_get_value(ui_SliderGunFan)) {
            lv_obj_remove_event_cb(ui_SliderGunFan, set_value_event_cb);
            lv_slider_set_value( ui_SliderGunFan, app_dev.para.run.gun_fan_gear, LV_ANIM_OFF);
            lv_obj_add_event_cb(ui_SliderGunFan, set_value_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
        }
    }    
    
    if(para_change_timeout>0) {
        para_change_timeout -= TIMER_PERIOD;
        if(para_change_timeout<=0) {
            xEventGroupSetBits(sys_event, kSysEvent_ParaChange);
            app_dev_para_init();
        }
    }
}


static void select_shortkey_back_event_cb(lv_event_t * e)
{
    uint16_t *value = (uint16_t *)(e->user_data);

    para_change_timeout = 2000; 
    app_dev.para.run.gun_temp = *value;
    xEventGroupSetBits(sys_event, kSysEvent_UIParaChange);
}

static void set_value_event_cb(lv_event_t * e)
{
    para_change_timeout = 2000; 
    app_dev.para.run.gun_fan_gear = lv_slider_get_value(ui_SliderGunFan);
    app_dev.para.run.gun_temp = lv_slider_get_value(ui_SliderGunSet)*10;
}

void gun_group_select(uint8_t id)
{
    uint8_t i;
    lv_timer_pause(lv_timer);
    switch(id){
    case 0:  
        lv_group_add_obj(g_encoder, ui_SliderGunSet);
        for(i=0;i!=SHORTKEY_NUM;i++)
            lv_group_add_obj(g_encoder, shortkey_btn[i]);
        lv_group_add_obj(g_encoder, ui_SliderGunFan);
        lv_timer_set_cb(lv_timer, gun_tab0_refresh);
        break;
    default:
        lv_timer_set_cb(lv_timer, NULL);
        break;
    }
    lv_timer_resume(lv_timer);
}
